<?php

//
// +---------------------------------------------------------+
// | 开发组
// +---------------------------------------------------------+
// | Copyrigh  (c)  2020 The PHP Group
// +---------------------------------------------------------+
// | 商户 代理商
// +---------------------------------------------------------+
// | zxc
// | php后端开发
// |
// +———————————————————+
//

namespace App\Models\Admin;

use App\Tools\Financial;
use App\Tools\Prstatic;
use Illuminate\Database\Eloquent\Model;

class Store extends Model
{

    protected $table = 'store';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U'; // 时间戳


    /**
     * 获取列表
     */
    public static function getPageList($where = [], $limit = 10)
    {
        $data[] = ['status', '>', 0];
        $data[] = ['status', '<', 10];
        if ($where['name']) {
            $data[] = ['store_name', 'like', "%{$where['name']}%"];
        }
        $start = $where['start_time'] ? strtotime($where['start_time']) : 0;
        $end = $where['end_time'] ? strtotime($where['end_time']) : time();
        if ($limit == 0) {
            $list = self::where($data)->whereBetween('apply_time', [$start, $end])->orderBy('id', 'desc')->get();
        } else {
            $list = self::where($data)->whereBetween('apply_time', [$start, $end])->orderBy('id', 'desc')->paginate($limit);
        }
        foreach ($list as $key => $vv) {
            $list[$key]['apply_time'] = $vv['apply_time'] ? date('Y-m-d H:i:s', $vv['apply_time']) : '--';
            $list[$key]['agent_end_time'] = $vv['agent_end_time'] ? date('Y-m-d', $vv['agent_end_time']) : '--';
            $list[$key]['status'] = Prstatic::$storeStatus[$vv['status']];
            $list[$key]['money_goods'] = Financial::getInfoByAid($vv['id'], 1, 1, 'money');
            $list[$key]['money_management'] = Financial::getInfoByAid($vv['id'], 1, 2, 'money');
        }
        return $list;
    }

    /**
     * 获取列表
     * @return mixed
     */
    public static function getBaseList()
    {
        return self::select(['id', 'store_name', 'discount_rate'])->where('status', '>', 0)->get();
    }

    /**
     * 获取列表
     */
    public static function getApplyList($where = [], $limit = 15)
    {
        $data = [];
        if ($where['name']) {
            $data[] = ['store_name', 'like', "%{$where['name']}%"];
        }
        $start = $where['start_time'] ? strtotime($where['start_time']) : 0;
        $end = $where['end_time'] ? strtotime($where['end_time']) : time();
        if ($limit == 0) {
            $list = self::where($data)->whereIn('status', [0, 11])->whereBetween('apply_time', [$start, $end])->orderBy('id', 'desc')->get();
        } else {
            $list = self::where($data)->whereIn('status', [0, 11])->whereBetween('apply_time', [$start, $end])->orderBy('id', 'desc')->paginate($limit);
        }
        foreach ($list as $key => $vv) {
            $list[$key]['apply_time'] = date('Y-m-d H:i:s', $vv['apply_time']);
            $list[$key]['status_name'] = Prstatic::$storeStatus[$vv['status']];
        }
        return $list;
    }

    /**
     * 获取所有列表
     * @param array $param
     * @param int $limit
     * @return mixed
     */
    public static function getAllList($param = [], $limit = 15)
    {
        $where = [];
        if ($param['name']) {
            $where[] = ['store_name', 'like', "%{$param['name']}%"];
        }
        if ($param['status']) {
            $where[] = ['status', '=', $param['status']];
        }

        if ($limit == 0) {
            $list = self::where($where)->orderBy('id', 'desc')->get();
        } else {
            $list = self::where($where)->orderBy('id', 'desc')->paginate($limit);
        }
        foreach ($list as $key => $vv) {
            $list[$key]['status'] = Prstatic::$storeStatus[$vv['status']];
        }
        return $list;
    }

    /**
     * 获取信息
     * @param $id
     */
    public static function getInfoById($id)
    {
        $info = self::where(['id' => $id])->first();
        if ($info) {
            $info['agent_start_time'] = date('Y-m-d 00:00:00', $info['agent_start_time']);
            $info['agent_end_time'] = date('Y-m-d 00:00:00', $info['agent_end_time']);
            $info['money_goods'] = Financial::getInfoByAid($info['id'], 1, 1, 'money');
            $info['money_management'] = Financial::getInfoByAid($info['id'], 1, 2, 'money');
            return $info->toArray();
        }
        return [];
    }

    /**
     * 创建代理商
     * @param $data
     */
    public static function addInfo($data)
    {
        $info['store_sn'] = $data['store_sn'];
        $info['store_name'] = $data['store_name'];
        $info['subsidiary'] = $data['subsidiary'];
        $info['money_goods'] = $data['money_goods'] ?: 0;
        $info['money_management'] = $data['money_management'] ?: 0;
        $info['principal_name'] = $data['principal_name'];
        $info['principal_mobile'] = $data['principal_mobile'];
        $info['principal_email'] = $data['principal_email'];
        $info['principal_address'] = $data['principal_address'];
        $info['principal_card_sn'] = $data['principal_card_sn'];
        $info['principal_card_a'] = $data['principal_card_a'];
        $info['principal_card_b'] = $data['principal_card_b'];
        $info['company_sn'] = $data['company_sn'];
        $info['company_license'] = $data['company_license'];
        $info['company_license_yy'] = $data['company_license_yy'] ?: '';
        $info['company_type'] = $data['company_type'];
        $info['company_name'] = $data['company_name'];
        $info['company_addrss'] = $data['company_addrss'];
        $info['company_corporation'] = $data['company_corporation'];
        $info['company_registered_capital'] = $data['company_registered_capital'];
        $info['company_create_time'] = $data['company_create_time'];
        $info['company_operating_period'] = $data['company_operating_period'];
        $info['company_business_scope'] = $data['company_business_scope'];
        $info['is_replenishment'] = $data['is_replenishment'] ?: 1;
        $info['discount_rate'] = $data['discount_rate'] ?: 1;
        $info['is_express_fee'] = $data['is_express_fee'] ?: 1;
        $info['is_agent'] = $data['is_agent'] ?: 0;
        $info['remark'] = $data['remark'] ?: '';
        $info['status'] = $data['status'] ?: 3;
        $info['created_at'] = time();
        $info['apply_time'] = time();
        $info['agent_start_time'] = $data['agent_start_time'];
        $info['agent_end_time'] = $data['agent_end_time'];
        $info['avoid_review_amount'] = $data['avoid_review_amount'] ?: 0;
        $info['avoid_review_start_time'] = $data['avoid_review_start_time'] ?: 0;
        $info['avoid_review_end_time'] = $data['avoid_review_end_time'] ?: 0;
        return self::insertGetId(array_filter($info));
    }

    /**
     * 修改信息
     * @param $id
     * @param $data
     */
    public static function editInfoById($id, $data)
    {
        $endTime = $data['agent_end_time'] ? strtotime($data['agent_end_time']) : 0;
        $endTimeMax = strtotime('20380101');
        if ($endTime >= $endTimeMax) {
            $endTime = $endTimeMax;
        }
        $info['store_sn'] = $data['store_sn'];
        $info['store_name'] = $data['store_name'];
        $info['subsidiary'] = $data['subsidiary'];
        $info['money_goods'] = $data['money_goods'] ?: 0;
        $info['money_management'] = $data['money_management'] ?: 0;
        $info['principal_name'] = $data['principal_name'];
        $info['principal_mobile'] = $data['principal_mobile'];
        $info['principal_email'] = $data['principal_email'] ?: '';
        $info['principal_address'] = $data['principal_address'] ?: '';
        $info['principal_card_sn'] = $data['principal_card_sn'];
        $info['principal_card_a'] = $data['principal_card_a'] ?: '';
        $info['principal_card_b'] = $data['principal_card_b'] ?: '';
        $info['company_sn'] = $data['company_sn'];
        $info['company_license'] = $data['company_license'] ?: '';
        $info['company_license_yy'] = $data['company_license_yy'] ?: '';
        $info['company_type'] = $data['company_type'] ?: '';
        $info['company_name'] = $data['company_name'] ?: '';
        $info['company_addrss'] = $data['company_addrss'] ?: '';
        $info['company_corporation'] = $data['company_corporation'] ?: '';
        $info['company_registered_capital'] = $data['company_registered_capital'] ?: '';
        $info['company_create_time'] = $data['company_create_time'] ?: '';
        $info['company_operating_period'] = $data['company_operating_period'] ?: '';
        $info['company_business_scope'] = $data['company_business_scope'] ?: '';
        $info['agent_start_time'] = $data['agent_start_time'] ? strtotime($data['agent_start_time']) : 0;
        $info['agent_end_time'] = $endTime;
        $info['is_replenishment'] = $data['is_replenishment'] ?: 0;
        $info['discount_rate'] = $data['discount_rate'] ?: 1;
        $info['is_express_fee'] = $data['is_express_fee'] ?: 0;
        $info['is_agent'] = $data['is_agent'] ?: 0;
        $info['remark'] = $data['remark'] ?: '';
        $info['status'] = $data['status'] ?: 3;
        $info['avoid_review_amount'] = $data['avoid_review_amount'] ?: 0;
        $info['avoid_review_start_time'] = $data['avoid_review_start_time'] ?: "";
        $info['avoid_review_end_time'] = $data['avoid_review_end_time'] ?: "";
        //        v($data);
        return self::where(['id' => $id])->update($info);
    }
}